//
//  HTLoginTableViewController.m
//  HTERPManApp
//
//  Created by standychen on 14-11-5.
//  Copyright (c) 2014年 HT. All rights reserved.
//

#import "htvlogin.h"
#import "HTFrameKit.h"
#import "HTSysKit.h"
#import "NavBarBackgroundGradientView.h"
#import "AppDelegate.h"
#import "XYAlertViewHeader.h"
#import "DXAlertView.h"



@interface htvlogin ()<UITableViewDelegate>
{
    BOOL isSeleted;
     MONActivityIndicatorView *indicatorView;
}


@end

@implementation htvlogin



- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}


#pragma mark --打开窗体执行
- (void)viewDidLoad
{
    [super viewDidLoad];
    
   
    [[self.uv layer]setCornerRadius:5];
    
    [[self.uv1 layer]setCornerRadius:5];
    [[self.uv2 layer]setCornerRadius:5];
    [[self.uv3 layer]setCornerRadius:5];
    
    [[self.loginButton layer]setCornerRadius:5];
    
    self.loginButton.frame=CGRectMake(15, 195, 381, 40);
   
    self.inCompField.text=@"HT";
    
    self.inCompField.delegate=self;
    self.inPasswordField.delegate=self;
    self.inUserField.delegate=self;
    
    //self.inCompField.autocapitalizationType=UITextAutocapitalizationTypeAllCharacters;
    
    //[self.inPasswordField becomeFirstResponder];
    
    
    //设置背景图片
    UIImage *img = [UIImage imageNamed:@"unchecked.png"];
    
    [self.selectButton setImage:img forState:UIControlStateNormal];
    
    
    
    isSeleted = NO;
    
    
    //设置密码
    self.inPasswordField.secureTextEntry=TRUE;
    //self.inCompField.autocapitalizationType=UITextAutocapitalizationTypeNone;
    
    self.navigationController.navigationBar.hidden=YES;
    
    self.hidesBottomBarWhenPushed=YES;
    
//    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:11/255 green:100/255 blue:150/255 alpha:1.0f];
    self.navigationController.navigationBar.translucent = NO;
   
    
    //读取缓存信息
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *company = [defaults objectForKey:@"comany"];
    NSString *username = [defaults objectForKey:@"username"];
    if(![company isEqualToString:@""] && company!=nil)//公司
    {
        self.inCompField.text=company;
    }
    
    self.inUserField.text=username;
    
    self.tableView.allowsSelection=NO;
    self.tableView.separatorStyle=NO;
    
    //[self.loginButton useGreenConfirmStyle];
    
    [self.tableView.tableHeaderView setHidden:YES];
    
        
   
    
    
    
    
    
    
    
    

    
    
    
    
}



- (void)textFieldDidBeginEditing:(UITextField *)textField {
    //[self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//Self.View手势
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //输入框放弃第一响应者
    [self.inCompField resignFirstResponder];//放弃键盘
    [self.inUserField resignFirstResponder];//放弃键盘
    [self.inPasswordField resignFirstResponder];//放弃键盘
}





//设置Section头部区域高度
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    CGFloat cg=20;
//    switch (section) {
//        case 0:cg=30;
//            break;
//            
//        case 1:
//            cg=0;
//        default:
//            break;
//    }
//    
//    return cg;
//}

//设置Section尾部区域高度
//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{CGFloat cg=20;
//    switch (section) {
//        case 0:cg=10;
//            break;
//            
//        case 1:
//            cg=30;
//        default:
//            break;
//    }
//    return cg;
//}

//改变自动登录勾选状态
- (IBAction)autoChange:(id)sender {
    UIImage *img;
    if (isSeleted) {
        img = [UIImage imageNamed:@"unchecked.png"];
    }
    else{
        img = [UIImage imageNamed:@"checked.png"];
    }
    
    [self.selectButton setImage:img forState:UIControlStateNormal];
    
    isSeleted = !isSeleted;

}

-(void)createActive{
    indicatorView = [[MONActivityIndicatorView alloc] init];
    indicatorView.delegate = self;
    indicatorView.numberOfCircles = 3;
    indicatorView.radius = 20;
    indicatorView.internalSpacing = 3;
    indicatorView.center = self.view.center;
    [indicatorView startAnimating];
    [self.view addSubview:indicatorView];
    [NSTimer scheduledTimerWithTimeInterval:0 target:indicatorView selector:@selector(startAnimating) userInfo:nil repeats:NO];
}


//登录
- (IBAction)loginAction:(id)sender {
    
    //[self createActive];
    [self.inCompField resignFirstResponder];
    [self.inPasswordField resignFirstResponder];
    [self.inUserField resignFirstResponder];
   
    
    //保存用户信息
    //用户配置对象
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    //保存用户名
    [defaults setObject:self.inCompField.text forKey:@"comany"];
    [defaults setObject:self.inUserField.text forKey:@"username"];
    
    if (isSeleted)
    {
        [defaults setObject:self.inPasswordField.text forKey:@"password"];
        //[defaults setObject:isSeleted forKey:@"autoflag"];
    }
    //写入文件，内部自动创建一个plist
    [defaults synchronize];
    
    [self.loginIndicator startAnimating];
    
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //code
        //sleep(5);
        //显示UI
        dispatch_async(dispatch_get_main_queue(), ^{
            //code ui
            [HTSysLogin hmCheckLogin_ASI:self.inCompField.text andUserName:self.inUserField.text andPassword:self.inPasswordField.text complete:^{
                
                AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
                if([myDelegate.backVersion isEqualToString:kpVersion]){
                    [HTSysParam autoLoginFlagSet:false];//非自动登录参数设置
                    
                    //跳转到主页面
                    [self performSegueWithIdentifier:@"loginsegue" sender:nil];
                    //[NSTimer scheduledTimerWithTimeInterval:0 target:indicatorView selector:@selector(stopAnimating) userInfo:nil repeats:NO];
                }
                else{
                    
                    
                    DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"提示" contentText:@"客户端需要更新最新版本！" leftButtonTitle:@"继续登录" rightButtonTitle:@"更新"];
                    [alert show];
                    alert.leftBlock = ^() {
                        [HTSysParam autoLoginFlagSet:false];//非自动登录参数设置
                         //跳转到主页面
                        [self performSegueWithIdentifier:@"loginsegue" sender:nil];
                        //[NSTimer scheduledTimerWithTimeInterval:0 target:indicatorView selector:@selector(stopAnimating) userInfo:nil repeats:NO];

                    };
                    alert.rightBlock = ^() {
                        NSString *appURL = [NSString stringWithFormat:@"http://www.icoix.com/ap/apone_ios.htm"];
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appURL]];

                    };

                    

                }
                
            } finally:^{
                 self.inPasswordField.text=@"";
                [self.inPasswordField becomeFirstResponder];
                [self.loginIndicator stopAnimating];
            }];
        });
        
    });
    
    //[self.loginIndicator stopAnimating];
    
}

-(IBAction)textFieldDoneEditing:(id)sender{
   
    [sender resignFirstResponder];
}

-(IBAction)backgroundTap:(id)sender{
    [self.inCompField resignFirstResponder];
    [self.inPasswordField resignFirstResponder];
    [self.inUserField resignFirstResponder];
}


//根据被点击按钮的索引处理点击事件
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex==0){
        [HTSysParam autoLoginFlagSet:false];//非自动登录参数设置
        
        //跳转到主页面
        [self performSegueWithIdentifier:@"loginsegue" sender:nil];
    }
    else{
        
        NSString *appURL = [NSString stringWithFormat:@"http://www.icoix.com/ap/apone_ios.htm"];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appURL]];
    }
}





@end
